<?php

namespace App\Classes;

use App\Filament\Enums\AssetType;
use App\Http\Controllers\OrderController;
use Filament\Facades\Filament;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Support\Enums\ActionSize;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\IconPosition;
use Filament\Support\Enums\IconSize;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables\Actions\Action as TableAction;
use Filament\Actions\Action;
use JaOcero\RadioDeck\Forms\Components\RadioDeck;
use App\Filament\Enums\PayType;
use App\Http\Controllers\WithdrawController;
use App\Models\User;
use Filament\Forms\Components\FileUpload;

class CommonAction
{
    public static function showBalanceAction($context)
    {
        $balance = Filament::auth()->getUser()->balance;
        if($context == 'tableheader'){
            $action = TableAction::make('showBalanceActionModal');
        }else{
            $action = Action::make('showBalanceAction');
        }

        return $action
            ->label("当前余额: ".$balance)
            ->badge()
            ->color('danger')
            ->size(ActionSize::ExtraLarge);
    }
    public static function buyPlanAction($context)
    {
        if($context == 'tableheader'){
            $action = TableAction::make('showBuyPlanModal');
        }else{
            $action = Action::make('showBuyPlanModal');
        }
        return $action
            ->label('购买套餐')
            ->action(function(array $arguments, array $data){
                $plan_id = $arguments['id'];
                $payment_method = $data['payment_method'];
                $coupon_code = $data['coupon_code'];
                $remark = $data['remark'];
                new OrderController($plan_id, $payment_method, $remark, $coupon_code);
            })->form([
                Radio::make('payment_method')
                    ->label('支付方式')
                    ->options([
                    'balance' => '余额',
                    'alipay' => '支付宝',
                    'wechat' => '微信',
                    'paypal' => 'PayPal',
                ])->default('balance')->inline(),
                TextInput::make('coupon_code')->label('优惠码'),
                Textarea::make('remark')->label('备注'),
            ])->modalAlignment(Alignment::Center)
            ->modalWidth(MaxWidth::TwoExtraLarge);
    }

    public static function rechargeAction($context)
    {
        if($context == 'tableheader'){
            $action = TableAction::make('rechargeBalance');
        }else{
            $action = Action::make('rechargeBalance');
        }

        return $action
            ->label('充值余额')
            ->action(function(array $arguments, array $data){

//                $amount = $data['amount'];
//                $payment_method = $data['payment_method'];
//                $remark = $data['remark'];
                //new RechargeController($payment_method, $amount, $remark);
            })->form([
                TextInput::make('amount')->label('金额'),
                RadioDeck::make('payment_method')
                    ->label('支付方式')
                    ->options(PayType::class)
                    ->descriptions(PayType::class)
                    ->icons(PayType::class)
                    ->required()
                    ->iconSize(IconSize::Large)
                    ->iconPosition(IconPosition::Before)
                    ->alignment(Alignment::Center)
                    ->color('danger')
                    ->extraCardsAttributes([ // Extra Attributes to add to the card HTML element
                        'class' => 'payment-method-btn',
                    ])->columns(3),
//                Textarea::make('remark')->label('备注'),
            ])->modalAlignment(Alignment::Center)
            ->modalWidth(MaxWidth::TwoExtraLarge);

     }

     public static function withdrawAction($context)
     {
         $balance = Filament::auth()->getUser()->balance;

         if($context == 'tableheader'){
             $action = TableAction::make('withdrawBalance');
         }else{
             $action = Action::make('withdrawBalance');
         }

         return $action
             ->label('余额提现')
             ->action(function(array $arguments, array $data){
                 $amount = $data['amount'];
                 $payment_method = $data['payment_method'];
                 new WithdrawController($payment_method, $amount);
             })->form([
                 TextInput::make('current_balance')->label('当前余额')->default($balance)->disabled(),
                 TextInput::make('amount')->label('提现金额'),
                 RadioDeck::make('payment_method')
                     ->label('提现方式')
                     ->options(PayType::class)
                     ->descriptions(PayType::class)
                     ->icons(PayType::class)
                     ->required()
                     ->iconSize(IconSize::Large)
                     ->iconPosition(IconPosition::Before)
                     ->alignment(Alignment::Center)
                     ->color('danger')
                     ->extraCardsAttributes([ // Extra Attributes to add to the card HTML element
                         'class' => 'payment-method-btn',
                     ])->columns(3),
//                Textarea::make('remark')->label('备注'),
             ])->modalAlignment(Alignment::Center)
             ->modalWidth(MaxWidth::TwoExtraLarge);
     }

     public static function editProfileAction($context)
     {
         if($context == 'tableheader'){
             $action = TableAction::make('editProfile');
         }else{
             $action = Action::make('editProfile');
         }

         $user = Filament::auth()->getUser();
         $user_id = $user["id"];

         return $action
             ->label('编辑资料')
             ->icon('heroicon-o-pencil')
             ->action(function(array $arguments, array $data){

                 $user = Filament::auth()->getUser();
                 $user_id = $user["id"];

                 User::find($user_id)->update([
                     'name' => $data['name'],
                 ]);

                 if(isset($data['avatar_url'])){
                     User::find($user_id)->update([
                         'avatar_url' => $data['avatar_url'],
                     ]);
                 }

                 Notification::make()->success()->title('编辑成功');

             })->form([
                 TextInput::make('email')->label('邮箱')->disabled()->default($user["email"]),
                 TextInput::make('name')->label('昵称')->default($user["name"]),
                 FileUpload::make('avatar_url')->label('头像')->avatar()->default($user["avatar_url"]),
                 //TextInput::make('email')->label('邮箱'),
             ]);
     }
}
